package com.neborosoft.FlyingDog.android.playing;

import android.app.Activity;
import android.media.MediaPlayer;
import android.widget.SeekBar;
import android.widget.TextView;
import com.neborosoft.FlyingDog.android.MyActivity;
import com.neborosoft.android.events.Event;

import java.sql.Time;
import java.util.HashSet;

/**
 * Created with IntelliJ IDEA.
 * User: CM
 * Date: 02.11.12
 * Time: 22:02
 * To change this template use File | Settings | File Templates.
 */
public class SeekBarAndDurationViewsUpdater implements Event {
    private HashSet<SeekBarAndDurationViewsHolder> requestedForUpdate
            = new HashSet<SeekBarAndDurationViewsHolder>();
    private MediaPlayer mediaPlayer;
    private OnPlayerSeekBarPositionChanged onPlayerSeekBarPositionChanged =
            new OnPlayerSeekBarPositionChanged();

    static private class SeekBarAndDurationViewsHolder{
        SeekBar seekBar;
        TextView durationStart,durationEnd;

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            SeekBarAndDurationViewsHolder that = (SeekBarAndDurationViewsHolder) o;

            if (!durationEnd.equals(that.durationEnd)) return false;
            if (!durationStart.equals(that.durationStart)) return false;
            if (!seekBar.equals(that.seekBar)) return false;

            return true;
        }

        @Override
        public int hashCode() {
            int result = seekBar.hashCode();
            result = 31 * result + durationStart.hashCode();
            result = 31 * result + durationEnd.hashCode();
            return result;
        }

        SeekBarAndDurationViewsHolder(SeekBar seekBar, TextView durationStart, TextView durationEnd) {
            this.seekBar = seekBar;
            this.durationStart = durationStart;
            this.durationEnd = durationEnd;
        }
    }

    private static String durationToString(int seconds){
        if(seconds < 0)
            return null;
        Time time = new Time(0,0,seconds);
        return time.toString();
    }

    private void updateDurationViews(TextView durationFirst, TextView durationEnd){
        String begin = durationToString(getCurrentPlayerPositionInSeconds());
        int intEnd = mediaPlayer.getDuration()/1000 - getCurrentPlayerPositionInSeconds();
        String end = durationToString(intEnd);

        if(begin != null){
            durationFirst.setText(begin);
        }
        if(end != null){
            durationEnd.setText(end);
        }
    }

    private void updateSeekBarProgress(SeekBar seekBar){
        double progress = (double)mediaPlayer.getCurrentPosition()/(double)mediaPlayer.getDuration();
        int intProgress = (int)(seekBar.getMax()*progress);
        seekBar.setProgress(intProgress);
    }

    private void updateOne(SeekBar seekBar, TextView durationFirst, TextView durationEnd){
        updateDurationViews(durationFirst, durationEnd);
        updateSeekBarProgress(seekBar);
    }

    public void update(){
        for(SeekBarAndDurationViewsHolder i : requestedForUpdate){
            updateOne(i.seekBar,i.durationStart,i.durationEnd);
        }
    }

    public void reset(int newDurationInSeconds){
        String durationZero = durationToString(0);
        String stringDuration = durationToString(newDurationInSeconds);
        for(SeekBarAndDurationViewsHolder i : requestedForUpdate){
            i.seekBar.setProgress(0);
            i.durationStart.setText(durationZero);
            i.durationEnd.setText(stringDuration);
        }
    }

    private int getCurrentPlayerPositionInSeconds(){
        return mediaPlayer.getCurrentPosition()/1000;
    }
    
    private void seekPlayerTo(double coefficient){
        int pos = (int)(mediaPlayer.getDuration()*coefficient);
        mediaPlayer.seekTo(pos);
    }
    
    public SeekBarAndDurationViewsUpdater(MediaPlayer mediaPlayer){
        this.mediaPlayer = mediaPlayer;
    }

    public void addSeekBarAndDurationViews(SeekBar seekBar,TextView begin,TextView end){
        requestedForUpdate.add(new SeekBarAndDurationViewsHolder(seekBar,begin,end));
        seekBar.setOnSeekBarChangeListener(onPlayerSeekBarPositionChanged);
    }

    public void removeSeekBarAndDurationViews(SeekBar seekBar,TextView begin,TextView end){
        requestedForUpdate.remove(new SeekBarAndDurationViewsHolder(seekBar, begin, end));
        seekBar.setOnSeekBarChangeListener(null);
    }

    private int lastPlayerPosition = -1;
    @Override
    public void run() {
        if(mediaPlayer.isPlaying())
        {
            int currentPlayerPosition = getCurrentPlayerPositionInSeconds();
            if(currentPlayerPosition == lastPlayerPosition)
                return;
            lastPlayerPosition = currentPlayerPosition;
            update();
        }
    }

    private class OnPlayerSeekBarPositionChanged implements SeekBar.OnSeekBarChangeListener{
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            if(!fromUser)
                return;
            double doubleProgress = (double)progress/(double)seekBar.getMax();
            seekPlayerTo(doubleProgress);
            update();
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    }
}
